//
//  MapViewController.m
//  ITASTRA
//
//  Created by me on 8/4/12.
//  Copyright (c) 2012 me. All rights reserved.
//

#import "MapViewController.h"
#import "MapViewAnnotation.h"

@implementation MapViewController

//@synthesize app;
@synthesize mapView;
@synthesize theList;


// When the view loads
- (void)viewDidLoad
{
	// Set some coordinates for our position (Buckingham Palace!)
	CLLocationCoordinate2D location;
	NSString *newLat = [theList.address_latitude  stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSString *newLng = [theList.address_longitude  stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    
    location.latitude = [newLat doubleValue];
	location.longitude = [newLng doubleValue];
    
 //   NSLog(@[newLat]);
 //   NSLog(@[newLng]);
   
 
    [self.mapView setShowsUserLocation:YES];
    
    MKCoordinateRegion zoomIn = mapView.region;
    zoomIn.span.latitudeDelta *= 0.5;
    [mapView setRegion:zoomIn animated:YES];
    			
	// Add the annotation to our map view
	MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:theList.name andCoordinate:location];
	[self.mapView addAnnotation:newAnnotation];
    [self.mapView setShowsUserLocation:YES];
}



- (IBAction)setMapType:(id)sender
{
    switch (((UISegmentedControl *)sender).selectedSegmentIndex)
    {
        case 0:
        {
            mapView.mapType = MKMapTypeStandard;
            break;
        }
        case 1:
        {
            mapView.mapType = MKMapTypeSatellite;
            break;
        }
        default:
        {
            mapView.mapType = MKMapTypeHybrid;
            break;
        }
    }
}




- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"itStr.png"]];
    imageView.frame = CGRectMake(0, 0, 320, 45); //change height and width accordingly
    [self.navigationController.navigationBar addSubview:imageView];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:194/255.0f green:194/255.0f blue:194/255.0f alpha:1];
    self.navigationController.toolbar.tintColor = [UIColor colorWithRed:194/255.0f green:194/255.0f blue:194/255.0f alpha:1];
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 320.0, 90.0);
}




// When a map annotation point is added, zoom to it (1500 range)
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
	MKAnnotationView *annotationView = [views objectAtIndex:0];
	id <MKAnnotation> mp = [annotationView annotation];
	MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 1500, 1500);
	[mv setRegion:region animated:YES];
	[mv selectAnnotation:mp animated:YES];
}

// Received memory warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// If the view unloads, release the map view
- (void)viewDidUnload {
	[super viewDidUnload];
	mapView = nil;
}

// Deallocations

- (IBAction)chooseMapType:(id)sender {
}
@end